import axios from 'axios'
import { Modal,message } from 'antd'
import Utils from "../utils/utils";


export default class Axios {

    static ajax2(options){

        return new Promise((resolve,reject) => {
            let baseApi='https://www.easy-mock.com/mock/5b38841791806979bd124639'
            axios({
                url:options.url,
                method:'get',
                baseURL:baseApi,
                timeout:5000,
                params:(options.data && options.data.params)||''  // 作判断,当参数有的时候，取参数，否则为空
            }).then((response)=> {
                if (response.status === 200){
                    let res = response.data
                    if (res.status === 200){
                        //成功
                        resolve(res)
                    }else {
                        Modal.info({
                            title:'提示',
                            content:response.message
                        })
                    }
                }else {
                    reject(response.data)
                }
            })
        })
    }


    static ajax(options){

        return new Promise((resolve,reject) => {
            // let baseApi='http://localhost:8005'
            let baseApi='http://localhost:9991/api'
            axios({
                url:options.url,
                method:options.method,
                baseURL:baseApi,
                timeout:5000,
                params:(options.data && options.data.params)||''  // 作判断,当参数有的时候，取参数，否则为空
            }).then((response)=> {
                // console.log('返回')
                if (response.status === 200){
                    let res = response.data
                    if (res.status === 200){
                        //成功
                        resolve(res)
                    }else {
                        Modal.info({
                            title:'提示',
                            content:response.message
                        })
                    }
                }else {
                    reject(response)
                }
            })
                .catch((error)=>{
                     reject(error)
                    message.error(`错误提示:[${error}]`)
                })
        })
    }


    static requestList(_this,url,method,params){
        const data={
            params:params
        }
        this.ajax({
            url:url,
            method:method,
            data:data
        }).then((res)=>{
            if (res && res.data){
                res.data.rows.map((item,index) => {
                    item.key = index
                    return item
                })
                _this.setState({
                    dataSource:res.data.rows,
                    selectedRowKeys:[],
                    selectedRows:null,
                    pagination:Utils.pagination(res,(current) => {
                        _this.params.page = current
                        _this.requestList()
                    })
                })
            }
        })
    }
}